import axios from 'axios';

// 创建axios实例
var url = 'http://39.108.151.99/api/';

const service = axios.create({
    baseURL: url, // api的base_url
    timeout: 30000 // 请求超时时间 30000
});


//添加请求拦截器
service.interceptors.request.use(function (config) {
    if (store.getters.token) {
        config.headers['TOKEN'] = getCookie('TOKEN')
    }
    return config
}, function (error) {
    return Promise.reject(error)
})


//添加响应拦截器
service.interceptors.response.use(
    response => {

        console.log(response, "===axios.response====");
        const result = response.data.RESULT;

        if (result.RESULT_CODE !== 1000) {
            console.log("reject");

            //举例 未登陆
            if (result.RESULT_CODE == 5001) {
                window.location.href = "http://192.168.0.10/login.html";
            }

            return Promise.reject(result);
        } else {
            console.log("resolve");
            return Promise.resolve(response.data.ROOT);
        }
    },
    error => {
        let ErrorMsg = error.message;
        if (error.code == 'ECONNABORTED') ErrorMsg = '请求超时，请稍后再试。';
        return Promise.reject(error);
    }
)

export default service;